package com.erp.erp_project.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.erp.erp_project.mapper.OrderMapper;
import com.erp.erp_project.model.Order;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

/**
 * @author the bright star
 */
@AllArgsConstructor
@Service
public class OrderService implements IOrderService{

    private OrderMapper orderMapper;

    @Override
    public List<Order> getAll() {
        QueryWrapper<Order> wrapper=new QueryWrapper<>();
        wrapper.isEmptyOfWhere();
        return orderMapper.selectList(wrapper);
    }

    @Override
    public Order getOrderWithOtherById(Integer id) {
        return orderMapper.selectWithOtherById(id);
    }

    @Override
    public List<Order> getOrdersWithOther() {
        return orderMapper.selectAllWithOther();
    }

    @Override
    public void updatePic(Integer id, String data) {
        orderMapper.updatePic(id,data);
    }

    @Override
    public List<String> uploadPic(MultipartFile[] files, Integer id) {
        StringBuilder builder=new StringBuilder();
        List<String> list=new ArrayList<>();
        for (MultipartFile file: files) {
            String oldName=file.getOriginalFilename();
            Long newName=System.currentTimeMillis();
            String folderPath="D:/Picture/pic/order/"+id+"/";
            String realPath=folderPath+newName+oldName.substring(oldName.lastIndexOf("."));
            File folder=new File(folderPath);
            if (!folder.exists()){
                folder.mkdirs();
            }
            File file1=new File(realPath);
            try {
                file.transferTo(file1);
            } catch (IOException e) {
                e.printStackTrace();
            }
            String path="/img/order/"+id+"/"+newName+oldName.substring(oldName.lastIndexOf("."));
            builder.append(","+path);
            list.add(path);
        }
        if (id!=null){
            Order order=orderMapper.selectWithOtherById(id);
            String data="";
            if (order.getImage()!=null&&!order.getImage().equals("")){
                data=order.getImage()+builder;
            }else {
                data= builder.substring(1);
            }
            orderMapper.updatePic(id,data);
        }
        return list;
    }

    @Override
    public void updateFile(Integer id, String data) {
        orderMapper.updateFile(id,data);
    }

    @Override
    public List<String> uploadFile(MultipartFile[] files, Integer id) {
        StringBuilder builder=new StringBuilder();
        List<String> list=new ArrayList<>();
        for (MultipartFile file: files) {
            String oldName=file.getOriginalFilename();
            String folderPath="D:/file/order/"+id+"/";
            String realPath=folderPath+oldName;
            File folder=new File(folderPath);
            if (!folder.exists()){
                folder.mkdirs();
            }
            File file1=new File(realPath);
            try {
                file.transferTo(file1);
            } catch (IOException e) {
                e.printStackTrace();
            }
            String path="/file/order/"+id+"/"+oldName;
            builder.append(","+path);
            list.add(path);
        }
        if (id!=null){
            Order order=orderMapper.selectWithOtherById(id);
            String data=order.getFiles()+builder;
            orderMapper.updateFile(id,data);
        }
        return list;
    }

    @Override
    public void addOrder(Order order) {
        orderMapper.insert(order);
    }

    @Override
    public void editOrder(Order order) {
        orderMapper.updateById(order);
    }

    @Override
    public void deleteOrders(List<Integer> ids) {
        orderMapper.deleteOrdersById(ids);
    }

    @Override
    public List<Order> searchOrder(Integer type, String condition) {
        String str="";
        if (type==2){
            str="custom_name";
        }else if (type==3){
            str="product_name";
        }else {
            str="order_id";
        }
        condition="%"+condition+"%";
        return orderMapper.searchOrder(str,condition);
    }

    @Override
    public void setRequirement(String requirement,Integer id) {
        orderMapper.setRequirement(requirement,id);
    }


}
